package com.zzb.myshowme.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zzb.myshowme.R;
import com.zzb.myshowme.bean.UserInfo;
import com.zzb.myshowme.presenter.UserCenterPresenter;
import com.zzb.myshowme.presenter.impl.UserCenterPresenterImpl;
import com.zzb.myshowme.utils.Base64ImageUtils;
import com.zzb.myshowme.utils.CustomToast;
import com.zzb.myshowme.view.IView.UserCenterView;
import com.zzb.myshowme.wiget.LoadingDialog;


public class UserCenterActivity extends BaseActivity implements View.OnClickListener, UserCenterView{

    UserCenterPresenter userCenterPresenter;
    private LoadingDialog dialog;
    ImageView back_imageV;
    ImageView user_imageV;
    TextView nickname_textV;
    TextView follow_number, follower_number, video_number;
    RelativeLayout setting_layout;

    UserInfo userinfo;
    public static final int USERCENTER_REQUEST_CODE = 1;
    public static final int USERCENTER_RESULT_CODE = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_center);

//        dialog = new LoadingDialog(this);
//        dialog.show("Loading...");
        initView();
        userCenterPresenter.initData();
    }

    private void initView(){
        back_imageV = (ImageView) findViewById(R.id.back_imageV);
        user_imageV = (ImageView) findViewById(R.id.user_imageV);
        nickname_textV = (TextView) findViewById(R.id.nickname_textV);
        follow_number = (TextView) findViewById(R.id.follow_number);
        follower_number = (TextView) findViewById(R.id.follower_number);
        video_number = (TextView) findViewById(R.id.video_number);
        setting_layout = (RelativeLayout) findViewById(R.id.setting_layout);

        back_imageV.setOnClickListener(this);
        user_imageV.setOnClickListener(this);
        setting_layout.setOnClickListener(this);

        userCenterPresenter = new UserCenterPresenterImpl(this);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i(TAG,"requestCode = " + requestCode + "-----resultCode = " + resultCode);
        if(requestCode == UserCenterActivity.USERCENTER_REQUEST_CODE && resultCode == UserInformationActivity.USERINFO_RESULT_CODE){
            String nickname = data.getStringExtra("nickName");
            String Baser64Image = data.getStringExtra("Baser64Image");
            String birthday = data.getStringExtra("birthday");
            String sex = data.getStringExtra("sex");
            String phone = data.getStringExtra("phone");
            String location = data.getStringExtra("location");
            String signature = data.getStringExtra("signature");

            Log.d("test", "2Baser64Image " + Baser64Image);
            nickname_textV.setText(nickname);
            if(!TextUtils.isEmpty(Baser64Image)) {
                user_imageV.setImageBitmap(Base64ImageUtils.getBitmapFromBase64String(Baser64Image));
            }

            this.nickName = nickname;
            this.userPhoto = Baser64Image;
            this.birthday = birthday;
            this.sex = sex;
            this.phone = phone;
            this.location = location;
            this.signature = signature;
        }
    }

    @Override
    public void onBackPressed() {
        finish();
        overridePendingTransition(R.anim.move_in_bottom, R.anim.move_out_top);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_imageV:
                finish();
                overridePendingTransition(R.anim.move_in_bottom, R.anim.move_out_top);
                break;
            case R.id.user_imageV:
                Intent intent = new Intent(UserCenterActivity.this, UserInformationActivity.class);
                intent.putExtra("userPhoto", userPhoto);
                intent.putExtra("nickName", nickName);
                intent.putExtra("birthday", birthday);
                intent.putExtra("sex", sex);
                intent.putExtra("phone", phone);
                intent.putExtra("location", location);
                intent.putExtra("signature", signature);
                startActivityForResult(intent, USERCENTER_REQUEST_CODE);
                break;
            case R.id.setting_layout:
                intent = new Intent(UserCenterActivity.this, SettingActivity.class);
                startActivity(intent);
                break;
        }
    }


    /**--------------P 返回到 v 的 回调接口 --------------------***/

    private String userPhoto, nickName, sex, birthday, phone, location, signature;
    @Override
    public void initData(UserInfo user){
        userPhoto = user.getUserPhoto();
        nickName = user.getNickName();
        sex = user.getSex();
        birthday = user.getBirthday();
        phone = user.getUser();
        location = user.getLocation();
        signature = user.getSignature();

        nickname_textV.setText(user.getNickName());
        if(!TextUtils.isEmpty(userPhoto)) {
            user_imageV.setImageBitmap(Base64ImageUtils.getBitmapFromBase64String(user.getUserPhoto()));
        } else {//头像为空 加载默认头像
            user_imageV.setImageResource(R.drawable.photo_default);
//            Bitmap bitmap = new BitmapFactory().decodeResource(getResources(), R.drawable.photo_default);
//            userPhoto = Base64ImageUtils.getBase64FromBitmap(bitmap);
        }
    }

    @Override
    public void showToastError(String msg){
        CustomToast.show(this, msg, Toast.LENGTH_LONG);
    }

}
